package com.iflytek.autofly.setting.biz.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.iflytek.autofly.setting.R;
import com.iflytek.autofly.setting.biz.MainActivity;
import com.iflytek.autofly.setting.biz.system.SystemControl;
import com.iflytek.autofly.setting.widget.LoadingAnimView;
import com.iflytek.autofly.utils.FlyLog;
import com.iflytek.sunflower.FlowerCollector;

/**
 * <b>Description: </b>广播的接受者<br/>
 * <b>Change History:</b><br/>
 * <table border="1">
 * <tr>
 * <td><b>user</b></td>
 * <td><b>date</b></td>
 * <td><b>time</b></td>
 * <td><b>reason</b></td>
 * </tr>
 * <tr>
 * <td>JunLuan@iflytek.com</td>
 * <td>2015年5月26日</td>
 * <td>上午11:36:14</td>
 * <td>创建此文件</td>
 * </tr>
 * </table>
 *
 * @author JunLuan@iflytek.com
 */
public class FlySettingReceiver extends BroadcastReceiver {

    public static final String DIRECT_CMD = "directCmd";
    public static final String DEFAULT_ACTION = "iflytek.intent.action.AUTOFLY_SETTING";
    private static final String TAG = "FlySettingReceiver";

    public void onReceive(final Context context, Intent intent) {

        FlyLog.d(TAG, intent.getAction());

        String cmd = null;
        String directCmd = null;

        /* 根据action确定fragment_name的值 */
        String action = intent.getAction();
        Boolean backThere = intent.getBooleanExtra("backHere", false);
        switch (action) {
            case "iflytek.intent.action.WIFI_SETTING":
                cmd = "network.wifi.wifiopen";
                break;
            case "iflytek.intent.action.BLUETOOTH_SETTING":
                cmd = "network.bluetooth.btopen";
                break;
            case "iflytek.intent.action.BLUETOOTH_SETTING.SCAN":
                cmd = "network.bluetooth.btscan";
                break;
            case "iflytek.intent.action.UPDATE_SYSTEM":
                break;
            case "iflytek.intent.action.WIFI_SETTING_CLOSE":
                cmd = "network.wifi.wifioff";
                break;
            case "iflytek.intent.action.BLUETOOTH_SETTING_CLOSE":
                cmd = "network.bluetooth.close";
                break;
            case DEFAULT_ACTION:
                cmd = intent.getStringExtra("cmd");
                directCmd = intent.getStringExtra(DIRECT_CMD);
                break;
            default:
                FlyLog.e(TAG, "invalid action:" + action);
                return;
        }

        if (!TextUtils.isEmpty(directCmd)) {
            switch (directCmd) {
                case "reboot":
                    LoadingAnimView.show(context, context.getString(R.string.sys_reboot_rebooting));
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                Thread.sleep(500);
                            } catch (InterruptedException e) {
                                FlyLog.e(TAG, e);
                            }
                            FlowerCollector.onEvent(context, context.getString(R.string.sys_reboot), "broadcast");
                            SystemControl sysCtl = new SystemControl(context);
                            sysCtl.reboot();
                        }
                    }).start();
                    break;
                case "restoreFactory":
                    LoadingAnimView.show(context, context.getString(R.string.sys_restorefactory_restoring));
                    SystemControl sysCtl2 = new SystemControl(context);
                    sysCtl2.restoreFactory();
                    FlowerCollector.onEvent(context, context.getString(R.string.sys_restorefactory), "broadcast");
                    break;
            }
            return;
        }

        if (TextUtils.isEmpty(cmd)) {
            FlyLog.e(TAG, "invalid cmd extracted. action=" + action);
            return;
        }

        Intent i = new Intent(context, MainActivity.class);
        FlySettingReceiver.ReturnConfig.cmd = cmd;
        FlySettingReceiver.ReturnConfig.returnHome = !backThere;
        FlyLog.d(TAG, "command=" + FlySettingReceiver.ReturnConfig.cmd + ",returnHome=" + FlySettingReceiver.ReturnConfig.returnHome);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
        return;

    }

    public static class ReturnConfig {
        public static boolean returnHome = true;
        public static String cmd = "";
    }
}